package c_admin

import (
	//"fmt"
	"gintest/models"
	"gintest/until"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
	//"time"
)

func Goods_list(c *gin.Context){
	page,_:=strconv.ParseUint(c.DefaultPostForm("page","1"),10,64)
	pagesize,_:=strconv.ParseUint(c.DefaultPostForm("limit","15"),10,64)
	keyword:=c.DefaultPostForm("keyword","")
	store_id,_:=strconv.ParseUint(c.DefaultPostForm("store_id","0"),10,64)
	status,_:=strconv.ParseUint(c.DefaultPostForm("status","0"),10,64)
	list,total:=models.GetGoodsList(page,pagesize,keyword,store_id,status)
	until.Success(c,map[string]interface{}{"data":list,"total":total})
}
func Goods_doadd(c *gin.Context){
	var form models.Goods
	if err:=c.ShouldBind(&form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	if len(form.Title)==0 {
		until.Fail(c,1,"title field cannot be empty")
		return
	}
	if form.StoreId<=0 {
		until.Fail(c,1,"storeid field cannot be empty")
		return
	}
	if form.CustomId<=0 {
		until.Fail(c,1,"customid field cannot be empty")
		return
	}

	
	//详情
	form.GoodsContent=models.GoodsContent{Content:c.DefaultPostForm("content","")}

	//默认规格
	var def_spc_form models.GoodsSpec
	if err:=c.ShouldBind(&def_spc_form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	if def_spc_form.Price<=0{
		until.Fail(c,1,"price field cannot be empty")
		return
	}
	image_map,_ := c.GetPostFormMap("image")
	for _,v:=range(image_map){
		def_spc_form.GoodsImage=append(def_spc_form.GoodsImage,models.GoodsImage{Image:v})
	}
	def_spc_form.Def=1

	form.GoodsSpec=[]models.GoodsSpec{def_spc_form}
	if err:=models.AddGoods(&form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)

}
func Goods_detail(c *gin.Context){
	id:=c.DefaultPostForm("id","")
	if len(id)==0{
		until.Fail(c,1,"id field cannot be empty")
		return
	}
	aid,_:=strconv.Atoi(id)
	data:=models.GetGoodsById(aid)
	until.Success(c,data)

}
func Goods_doedit(c *gin.Context){
	var form models.Goods
	if err:=c.ShouldBind(&form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	if len(form.Title)==0 {
		until.Fail(c,1,"title field cannot be empty")
		return
	}
	if form.CustomId<=0 {
		until.Fail(c,1,"customid field cannot be empty")
		return
	}

	
	//详情
	form.GoodsContent=models.GoodsContent{Content:c.DefaultPostForm("content","")}

	//默认规格
	var def_spc_form models.GoodsSpec
	if err:=c.ShouldBind(&def_spc_form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	if def_spc_form.Price<=0{
		until.Fail(c,1,"price field cannot be empty")
		return
	}
	image_map,_ := c.GetPostFormMap("image")
	for _,v:=range(image_map){
		def_spc_form.GoodsImage=append(def_spc_form.GoodsImage,models.GoodsImage{Image:v})
	}
	def_spc_form.Def=1
	def_spc_form.Id,_=strconv.Atoi(c.DefaultPostForm("spec_id","0"))

	if def_spc_form.Id<=0 {
		until.Fail(c,1,"spec_id field cannot be empty")
		return
	}

	form.GoodsSpec=[]models.GoodsSpec{def_spc_form}

	if err:=models.EditGoodsById(&form);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)


}
func Goods_del(c *gin.Context){
	idsstr:=c.DefaultPostForm("id","")
	idsstr_arr := strings.Split(idsstr, ",")
	var ids []int
	for i:=0;i<len(idsstr_arr);i++{
		intt,_:=strconv.Atoi(idsstr_arr[i])
		ids=append(ids,intt)
	}
	if err:=models.DelGoodsByIds(ids);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c,nil)
}
func Goods_Status(c *gin.Context){
	id,_:=strconv.ParseUint(c.DefaultPostForm("id","0"),10,64)
	status,_:=strconv.ParseUint(c.DefaultPostForm("status","0"),10,64)
	if id<=0 {
		until.Fail(c,1,"id field cannot be empty")
		return
	}
	if status<=0 {
		until.Fail(c,1,"status field cannot be empty")
		return
	}
	if err:=models.EditGoodsStatusById(id,status);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c,nil)
}
//接收json字符串
func GoodsAttr_add(c *gin.Context){
	var data []models.GoodsAttr
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	if err:=models.SaveGoodsAttr(data,false);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)
}
//接收json字符串
func GoodsAttr_save(c *gin.Context){
	var data []models.GoodsAttr
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	if err:=models.SaveGoodsAttr(data,true);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)

}
func GoodsAttr_del(c *gin.Context){
	id,_:=strconv.Atoi(c.DefaultPostForm("id","0"))
	if id<=0{
		until.Fail(c,1,"id field cannot be empty")
		return
	}
	if err:=models.DelGoodsAttr(id);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)
}
//接收json字符串
func GoodsSpec_save(c *gin.Context){
	var data []models.GoodsSpec
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	for i:=0;i<len(data);i++{
		data[i].Def=2
	}
	
	if err:=models.SaveGoodsSpec(data);err!=nil{
		until.Fail(c,1,err.Error())
		return
	}
	until.Success(c, nil)
}